package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllDetects", query = "select myDetect from Detect myDetect"),
		@NamedQuery(name = "findDetectByName", query = "select myDetect from Detect myDetect where myDetect.name = ?1"),
		@NamedQuery(name = "findDetectByNameContaining", query = "select myDetect from Detect myDetect where myDetect.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findDetectByDate", query = "select myDetect from Detect myDetect where myDetect.date = ?1"),
		@NamedQuery(name = "findDetectByConclusion", query = "select myDetect from Detect myDetect where myDetect.conclusion = ?1"),
		@NamedQuery(name = "findDetectByConclusionContaining", query = "select myDetect from Detect myDetect where myDetect.conclusion like concat('%',?1,'%')"),
		@NamedQuery(name = "findDetectByDescription", query = "select myDetect from Detect myDetect where myDetect.description = ?1"),
		@NamedQuery(name = "findDetectByDescriptionContaining", query = "select myDetect from Detect myDetect where myDetect.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllDetectsByDetectdepartment", query = "select myDetect from Detect myDetect where myDetect.detectdepartment.id = ?1"),
		@NamedQuery(name = "findAllDetectsByFoodbatch", query = "select myDetect from Detect myDetect where myDetect.foodbatch.id = ?1"),
		@NamedQuery(name = "findAllDetectsByDetector", query = "select myDetect from Detect myDetect where myDetect.detector.id = ?1"),
		@NamedQuery(name = "findAllDetectsBySubject", query = "select myDetect from Detect myDetect where myDetect.subject.id = ?1"),
		@NamedQuery(name = "findDetectById", query = "select myDetect from Detect myDetect where myDetect.id = ?1"),
		@NamedQuery(name = "findDetectByPrimaryKey", query = "select myDetect from Detect myDetect where myDetect.id = ?1 "),
		@NamedQuery(name = "deleteDetectsByIds", query = "delete Detect where id in (?1)"),
		@NamedQuery(name = "findDetectsByIds", query = "select myDetect from Detect myDetect where myDetect.id in (?1)"),
		@NamedQuery(name = "findDetectByPhyscialPrimaryKey", query = "select myDetect from Detect myDetect where  myDetect.subject.id = ?1 and myDetect.foodbatch.id = ?2 and myDetect.date = ?3 and myDetect.detector.id = ?4 and myDetect.detectdepartment.id = ?5"),
		@NamedQuery(name = "countDetectsByName", query = "select count(myDetect)  from Detect myDetect where myDetect.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "detect")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Detect")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Detect implements Serializable, Comparable<Detect> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar date;

	@Column(name = "conclusion")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String conclusion;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "detectdepartment", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Detectdepartment detectdepartment;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "foodbatch", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Foodbatch foodbatch;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "detector", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	User detector;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "subject", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Subject subject;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "detect", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectdetail> detectdetailsForDetect;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "detect", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectfeature> detectfeaturesForDetect;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}

	public Calendar getDate() {
		return this.date;
	}

	public void setConclusion(String conclusion) {
		this.conclusion = conclusion;
	}

	public String getConclusion() {
		return this.conclusion;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDetectdepartment(Detectdepartment detectdepartment) {
		this.detectdepartment = detectdepartment;
	}

	public Detectdepartment getDetectdepartment() {
		return detectdepartment;
	}

	public void setFoodbatch(Foodbatch foodbatch) {
		this.foodbatch = foodbatch;
	}

	public Foodbatch getFoodbatch() {
		return foodbatch;
	}

	public void setDetector(User detector) {
		this.detector = detector;
	}

	public User getDetector() {
		return detector;
	}

	public void setSubject(Subject subject) {
		this.subject = subject;
	}

	public Subject getSubject() {
		return subject;
	}

	public void setDetectdetailsForDetect(Set<Detectdetail> detectdetailsForDetect) {
		this.detectdetailsForDetect = detectdetailsForDetect;
	}

	public Set<Detectdetail> getDetectdetailsForDetect() {
		if (detectdetailsForDetect == null) {
			detectdetailsForDetect = new java.util.LinkedHashSet<Detectdetail>();
		}
		return detectdetailsForDetect;
	}

	public void setDetectfeaturesForDetect(Set<Detectfeature> detectfeaturesForDetect) {
		this.detectfeaturesForDetect = detectfeaturesForDetect;
	}

	public Set<Detectfeature> getDetectfeaturesForDetect() {
		if (detectfeaturesForDetect == null) {
			detectfeaturesForDetect = new java.util.LinkedHashSet<Detectfeature>();
		}
		return detectfeaturesForDetect;
	}

	public void copy(Detect that) {

		setId(that.getId());

		setName(that.getName());
		setDate(that.getDate());
		setConclusion(that.getConclusion());
		setDescription(that.getDescription());

		setDetectdepartment(that.getDetectdepartment());

		setFoodbatch(that.getFoodbatch());

		setDetector(that.getDetector());

		setSubject(that.getSubject());

		setDetectdetailsForDetect(
				new java.util.LinkedHashSet<Detectdetail>(that.getDetectdetailsForDetect()));

		setDetectfeaturesForDetect(new java.util.LinkedHashSet<Detectfeature>(
				that.getDetectfeaturesForDetect()));

	}

	public Detect() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("date=[").append(date).append("] ");
		buffer.append("conclusion=[").append(conclusion).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Detect))
			return false;
		Detect comparedDetect = (Detect) obj;
		if (!CheckPropertyEqual(this.subject.id, comparedDetect.subject.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.foodbatch.id, comparedDetect.foodbatch.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.date, comparedDetect.date)) {
			return false;
		}
		if (!CheckPropertyEqual(this.detector.id, comparedDetect.detector.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.detectdepartment.id, comparedDetect.detectdepartment.id)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Detect o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
